package ca.etsmtl.log660.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;

import ca.etsmtl.log660.entity.Film;
import ca.etsmtl.log660.servlet.RechercheFilmControler;

public class RechercheFilm extends JPanel{

	private static final long serialVersionUID = 1L;

	private RechercheFilmControler controler = new RechercheFilmControler();
	
	private JTextField paysField;
	private JTextField titreField;
	private JTextField realisateurField;
	private JTextField acteurField;
	private JTextField langueField;
	private JTextField genreField;
	private JTextField minBox;
	private JTextField maxBox;
	JButton detailsButton;
	private DefaultListModel modele;
	private JList listFilm;
	
	public RechercheFilm() {
		JLabel lblLogin = new JLabel("Acteur:");
		
		JLabel lblPassword = new JLabel("Ann\u00E9e de sortie:");
		
		JLabel lblPays = new JLabel("Pays:");
		
		JLabel lblLangueDorigine = new JLabel("Langue d'origine:");
		
		JLabel lblGenre = new JLabel("Genre:");
		
		minBox = new JTextField(4);	
		
		maxBox = new JTextField(4);
		
		paysField = new JTextField();
		paysField.setColumns(10);
		
		JLabel titreLabel = new JLabel("Titre:");
		
		JLabel realisateurLabel = new JLabel("Realisateur:");
		
		titreField = new JTextField();
		titreField.setColumns(10);
		
		realisateurField = new JTextField();
		realisateurField.setColumns(10);
		
		acteurField = new JTextField();
		acteurField.setColumns(10);
		
		JButton rechercherButton = new JButton("Rechercher");
		rechercherButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				rechercherBoutonAction();
			}
		});
		
		langueField = new JTextField();
		langueField.setColumns(10);
		
		genreField = new JTextField();
		genreField.setColumns(10);
		
		listFilm = new JList();
		modele = new DefaultListModel();
		listFilm.setModel(modele);
		listFilm.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
		JScrollPane jScrollPane1= new JScrollPane();
		jScrollPane1.setViewportView(listFilm);
		
		detailsButton = new JButton("Details");
		detailsButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				detailsBoutonAction();
			}
		});
		detailsButton.setEnabled(false);
		
		GroupLayout gl_panel = new GroupLayout(this);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(71)
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(lblGenre, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_panel.createSequentialGroup()
									.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
										.addComponent(lblPassword)
										.addComponent(lblPays, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
										.addComponent(titreLabel)
										.addComponent(realisateurLabel)
										.addComponent(lblLogin))
									.addGap(55)
									.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_panel.createSequentialGroup()
											.addComponent(minBox, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
											.addGap(18)
											.addComponent(maxBox, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
											.addComponent(acteurField, Alignment.LEADING)
											.addComponent(realisateurField)
											.addComponent(titreField, Alignment.LEADING)
											.addComponent(paysField, Alignment.LEADING)
											.addComponent(genreField, GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
											.addComponent(langueField)
											.addComponent(detailsButton, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE))))
								.addGroup(gl_panel.createSequentialGroup()
									.addGap(5)
									.addComponent(lblLangueDorigine))))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(178)
							.addComponent(rechercherButton))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(51)
							.addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 269, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(42, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
							.addGap(18)
							.addComponent(titreLabel)
						.addComponent(titreField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(realisateurLabel)
						.addComponent(realisateurField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblLogin)
						.addComponent(acteurField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(36)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblPassword)
						.addComponent(minBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(maxBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblPays)
						.addComponent(paysField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblLangueDorigine)
						.addComponent(langueField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(26)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblGenre)
						.addComponent(genreField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addComponent(rechercherButton)
					.addGap(33)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(detailsButton))
					.addContainerGap(52, Short.MAX_VALUE))
		);
		this.setLayout(gl_panel);
	}
	
	private void rechercherBoutonAction(){
		List<Film> films=controler.getResultatRecherche(titreField.getText(),minBox.getText(), maxBox.getText(), paysField.getText(), langueField.getText(), genreField.getText(), realisateurField.getText(), acteurField.getText());
		
		modele.removeAllElements();
        int position = 0;
        for (Film film : films) {
            position =modele.getSize();
            modele.add(position, film);
            detailsButton.setEnabled(true);
        }
        if(modele.size()==0){
        	JOptionPane.showMessageDialog(this, "Votre recherche retourne aucun Film!");
        }
	}
	
	private void detailsBoutonAction(){
		controler.selectionnerFilm((Film)listFilm.getSelectedValue());
	}
}
